﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;

namespace ProjectSchedule.Common
{

    /// <summary>
    /// 日志帮助类
    /// </summary>
    public class LogHelper
    {
        /// <summary>
        /// file log
        /// </summary>
        public const string LogConfigName = "log4net.FileLog";
        //调试开关，为"On"时记录debug日志
        private static readonly string DebugFlag = ConfigurationManager.AppSettings["EnabledLog"];

        /// <summary>
        /// debug log 
        /// </summary>
        /// <param name="message">message</param>
        public static void Debug(string message)
        {
            if (DebugFlag != null && DebugFlag == "On")
            {
                log4net.ILog log = log4net.LogManager.GetLogger(LogConfigName);
                if (log.IsDebugEnabled)
                {
                    log.Debug(message);
                }
                log = null;
            }
        }

        /// <summary>
        /// when error log
        /// </summary>
        /// <param name="message">message</param>
        public static void Error(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(LogConfigName);
            if (log.IsErrorEnabled)
            {
                log.Error(message);
            }
            log = null;
        }

        /// <summary>
        /// when exception log
        /// </summary>
        /// <param name="message"></param>
        /// <param name="ex"></param>
        public static void Error(string message, Exception ex)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(LogConfigName);
            if (log.IsErrorEnabled)
            {
                log.Error(message, ex);
            }
            log = null;
        }

        /// <summary>
        /// Error Log
        /// </summary>
        /// <param name="className"></param>
        /// <param name="methodName"></param>
        /// <param name="message"></param>
        public static void Error(string className, string methodName, string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(LogConfigName);
            if (log.IsErrorEnabled)
            {
                log.Error("Class Name:" + className + "\r\n");
                log.Error("Method Name:" + methodName + "\r\n");
                log.Error("Info:" + message);
            }
            log = null;
        }

        /// <summary>
        /// when fatal log
        /// </summary>
        /// <param name="message">message</param>
        public static void Fatal(string message)
        {

            log4net.ILog log = log4net.LogManager.GetLogger(LogConfigName);
            if (log.IsFatalEnabled)
            {
                log.Fatal(message);
            }
            log = null;
        }

        /// <summary>
        /// when info log
        /// </summary>
        /// <param name="message">message</param>
        public static void Info(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(LogConfigName);
            if (log.IsInfoEnabled)
            {
                log.Info(message);
            }
            log = null;
        }

        /// <summary>
        /// when warn log
        /// </summary>
        /// <param name="message">message</param>
        public static void Warn(string message)
        {
            log4net.ILog log = log4net.LogManager.GetLogger(LogConfigName);
            if (log.IsWarnEnabled)
            {
                log.Warn(message);
            }
            log = null;
        }
    }
}
